#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
	// 创建具名管道
	if(access("/tmp/rose_fifo", F_OK))      //rose说
	{
		mkfifo("/tmp/rose_fifo", 0666);
	}
    // 创建具名管道
	if(access("/tmp/jack_fifo", F_OK))      //jack说
	{
		mkfifo("/tmp/jack_fifo", 0666);
	}
	
	
	// 从管道读出数据
	int fd_jack = open("/tmp/jack_fifo", O_RDWR);
    int fd_rose = open("/tmp/rose_fifo", O_RDWR);

	char lis[1024];
    char say[1024];
	
    pid_t pid = fork();
    if (pid == 0)
    {
        while (1)
        {
            bzero(lis, 1024);
		
            read(fd_rose, lis, 1024);
            
            printf("roes:%s", lis);
            
            if(strncmp(lis, "byebye", 4) == 0)
                break;

        }
        
    }
    if (pid > 0)
    {
        while(1)
        {
            bzero(say, 1024);
            //printf("请输入想要发送的消息(qiut)：");
            fgets(say, 1024, stdin);
            
            write(fd_jack, say, strlen(say));
            
            if(strncmp(say, "byebye", 4) == 0)
                break;
            
            usleep(1000);
        }
    }
    
	close(fd_jack);
    close(fd_rose);
	return 0;
}